import { register } from '../../services/user'

Page({
  data: {
    pwdFocused: false,
    emailFocused: false,
    disabled: false,
    registering: false,
    username: {
      empty: true,
      invalid: false,
      msg: ''
    },
    pwd: {
      empty: true,
      invalid: false,
      msg: ''
    },
    email: {
      empty: true,
      invalid: false,
      msg: ''
    }
  },
  focusPwd: function (e) {
    this.setData({
      'pwdFocused': true
    })
  },
  focusEmail: function (e) {
    this.setData({
      'emailFocused': true
    })
  },
  usernameChange: function ({ detail: { value } }) {
    const v = value.trim()
    if (v === '') {
      this.setData({
        'username.empty': true,
        'username.msg': '请输入用户名',
        'username.invalid': true
      })
    } else if (v.length < 3) {
      this.setData({
        'username.empty': false,
        'username.msg': '请输入至少三个字符',
        'username.invalid': true
      })
    } else if (!new RegExp('[a-zA-Z]\\w+').test(v)) {
      this.setData({
        'username.empty': false,
        'username.msg': '用户名必须由字母开头且只包含字母、数字和下划线',
        'username.invalid': true
      })
    } else {
      this.setData({
        'username.empty': false,
        'username.invalid': false
      })
    }
  },
  pwdChange: function ({ detail: { value } }) {
    const v = value.trim()
    if (v === '') {
      this.setData({
        'pwd.empty': true,
        'pwd.msg': '请输入密码',
        'pwd.invalid': true
      })
    } else if (v.length < 6) {
      this.setData({
        'pwd.empty': false,
        'pwd.msg': '请输入至少六个字符',
        'pwd.invalid': true
      })
    } else {
      this.setData({
        'pwd.empty': false,
        'pwd.invalid': false
      })
    }
  },
  emailChange: function ({ detail: { value } }) {
    const v = value.trim()
    if (v === '') {
      this.setData({
        'email.empty': true,
        'email.msg': '请输入邮箱',
        'email.invalid': true
      })
    } else if (!new RegExp('^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$').test(v)) {
      this.setData({
        'email.empty': false,
        'email.msg': '请输入正确的邮箱',
        'email.invalid': true
      })
    } else {
      this.setData({
        'email.empty': false,
        'email.invalid': false
      })
    }
  },
  formSubmit: function ({ detail: { value } }) {
    if (this.data.username.invalid || this.data.pwd.invalid || this.data.email.invalid) {
      return
    }
    if (this.data.registering) {
      return
    }

    this.setData({ registering: true })
    register(value).then((data) => {
      wx.showToast({
        title: '注册成功，请登录',
        icon: 'success',
        success: function () {
          wx.navigateTo({
            url: '../login/index'
          })
        }
      })
      this.setData({ registering: false })
    }).catch(({ code }) => {
      if (code && code === 2001) {
        wx.showToast({
          title: '该用户名已被注册',
          icon: 'none'
        })
      }
      this.setData({ registering: false })
    })
  }
})